let app = getApp()
import api from '../../../api/index'
Page({
    data: {
        list: [],
        nowIndex: 0,
        isNext: true,
        size: 1,
        answerList: [],
        showStopDialog: false,
        countDownCom: null,
    },
    onShow() {
        let userInfo = wx.getStorageSync('userInfo')
        if (userInfo) {
            this.setData({
                list: [],
                nowIndex: 0,
                isNext: true,
                size: 1,
                answerList: []
            })
            this.getAnswerList()
        } else {
            app.$util.errorToShow("请先登录")
        }
    },
    onReady() {
        this.setData({
            countDownCom: this.selectComponent('#countDownId')
        })
    },
    mNext: app.$util.throttle(function () {
        if (this.data.nowIndex + 1 < this.data.size) {
            this.data.answerList.push(this.data.list[this.data.nowIndex + 1])
            console.log(this.data.answerList)
            this.setData({
                answerList: this.data.answerList,
                isNext: true,
                nowIndex: this.data.nowIndex + 1
            })
        } else {
            wx.showModal({
                title: '提示',
                content: '没有更多的题了，是否提交测试？',
                success: (res) => {
                    if (res.confirm) {
                        let intervalTime = this.data.countDownCom.getIntervalTime()
                        this.goResultPage(intervalTime)
                    }
                }
            })
        }
    }, 600),
    mBack: app.$util.throttle(function () {
        if (this.data.nowIndex > 0) {
            this.setData({
                isNext: false
            })
            setTimeout(() => {
                this.data.answerList.splice(-1, 1)
                this.setData({
                    answerList: this.data.answerList
                })
            }, 600)
            this.setData({
                nowIndex: this.data.nowIndex - 1
            })
        } else {
            app.$util.errorToShow("到底了")
        }
    }, 600),
    onShow() {
        this.getAnswerList()
    },
    // 获取题目列表
    async getAnswerList() {
        let than = this
        let list = [
            // {
            //     name: '苹果',
            //     type: '有害垃圾',
            //     choose: '可回收物'
            // },
            // {
            //     name: '苹果',
            //     type: '有害垃圾',
            //     choose: '可回收物'
            // }
        ]
        // await api.getRubbish().then(res=>{
        //     console.log(res.data.rows);
        //     list = res.data.rows
        // })
        wx.request({
          url: 'http://localhost:8080/api/dati/list',
          method:'GET',
          success(res){
            console.log(res.data);
            list  = res.data
            list.forEach(item => {
                item.choose = ''
            })
            than.setData({
                list: list,
                answerList: [list[0]],
                size: list.length
            })
          }
        })
       
    },
    changeHander(e) {
        let val = e.detail.value
        this.data.list[this.data.nowIndex].choose = val
        this.setData({
            list: this.data.list
        }, () => {
            this.mNext()
        })
    },
    stopTimeHandle(e) {
        let intervalTime = this.data.countDownCom.getIntervalTime()
        wx.showModal({
            title: '提示',
            content: '答题时间结束，请停止作答，提交答题！',
            showCancel: false,
            success: (res) => {
                if (res.confirm) {
                    this.goResultPage(intervalTime)
                }
            }
        })
    },
    goResultPage(intervalTime) {
        console.log(this.data.list);
        app.$util.redirectTo(`/pages/result/result?list=${JSON.stringify(this.data.list)}&intervalTime=${intervalTime}`)
    },
    // 页面卸载
    onUnload() {
        this.data.countDownCom.clearTimter()
    }
})